//
//  XQCityManager.m
//  JoinPeso
//
//  Created by 小琦 on 2024/12/3.
//

#import "XQCityManager.h"

@implementation XQCityManager

+ (NSArray *)getProvinceList {
    NSArray *list = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCitys"];
    if (list.count == 0) return @[];
    NSMutableArray *datas = [NSMutableArray array];
    for (NSDictionary *dic in list) {
        [datas addObject:dic[@"chance"]];
    }
    return datas;
}

+ (NSInteger)getProvinceIndex:(NSString *)province {
    NSArray *list = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCitys"];
    NSInteger selectIndex = 0;
    for (NSDictionary *dic in list) {
        if ([dic[@"chance"] isEqualToString:province]) {
            selectIndex = [list indexOfObject:dic];
            break;;
        }
    }
    return selectIndex;
}

+ (NSArray *)getCityListAtProvinceIndex:(NSInteger)provinceIndex {
    NSArray *list = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCitys"];
    if (provinceIndex >= list.count) {
        return @[];
    }

    id provinceDic = list[provinceIndex];
    NSMutableArray *datas = [NSMutableArray array];
    if ([provinceDic isKindOfClass:[NSDictionary class]]) {
        NSArray *cityList = provinceDic[@"rakuten"];
        for (NSDictionary *dic in cityList) {
            [datas addObject:dic[@"chance"]];
        }
    }
    return datas;
}

+ (NSInteger)getCityIndex:(NSInteger)provinceIndex cityName:(NSString *)cityName {
    NSArray *list = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCitys"];
    if (provinceIndex >= list.count) {
        return 0;
    }
    id provinceDic = list[provinceIndex];
    NSInteger cityIndex = 0;
    if ([provinceDic isKindOfClass:[NSDictionary class]]) {
        NSArray *cityList = provinceDic[@"rakuten"];
        for (NSDictionary *dic in cityList) {
            if ([dic[@"chance"] isEqualToString:cityName]) {
                cityIndex = [cityList indexOfObject:dic];
                break;
            }
        }
    }
    return cityIndex;
}

+ (NSArray *)getDistrictListAtProvinceIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex {
    NSArray *list = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCitys"];
    if (provinceIndex >= list.count) {
        return [self getDistrictListAtProvinceIndex:list.count - 1 cityIndex:cityIndex];
    }
    NSDictionary *provinceDic = list[provinceIndex];
    NSArray *cityList = provinceDic[@"rakuten"];
    if (cityIndex >= cityList.count) {
        return [self getDistrictListAtProvinceIndex:provinceIndex cityIndex:cityList.count - 1];
    }

    NSDictionary *cityDic = cityList[cityIndex];
    NSArray *districtList = cityDic[@"rakuten"];
    
    NSMutableArray *datas = [NSMutableArray array];
    for (NSDictionary *dic in districtList) {
        [datas addObject:dic[@"chance"]];
    }
    return datas;
}

+ (NSInteger)getDistrictIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex districtName:(NSString *)districtName {
    NSArray *list = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCitys"];
    if (provinceIndex >= list.count) {
        return 0;
    }
    NSDictionary *provinceDic = list[provinceIndex];
    NSArray *cityList = provinceDic[@"rakuten"];
    if (cityIndex >= cityList.count) {
        return 0;
    }

    NSDictionary *cityDic = cityList[cityIndex];
    NSArray *districtList = cityDic[@"rakuten"];
    
    NSInteger districtIndex = 0;
    for (NSDictionary *dic in districtList) {
        if ([dic[@"chance"] isEqualToString:districtName]) {
            districtIndex = [districtList indexOfObject:dic];
            break;
        }
    }
    return districtIndex;
}


@end
